<?php
if (!defined('ROOT_PATH')) die('No direct access.');
if ((isset($param[1])) && ($param[1]=='out')){	//logout
	unset($_SESSION['logged']);
	$facebook->destroySession();	//mengulang ke kondisi 0
	header('Location: ' . $_SERVER['HTTP_REFERER']);	//redirect ke halaman sebelumnya
}else if ((isset($param[1])) && ($param[1]=='reg')){	//register
	addUser($_POST['idfb'],$_POST['fb_username'],$_POST['nama'],$_POST['kontak'],$_POST['email'],$_POST['desc'],$_POST['token'],$_POST['kota'],$_POST['alamat']);
	$user = getUser($_POST['idfb']);
	$_SESSION['logged']['index'] = $user['idPengguna'];
	$_SESSION['logged']['id'] = $user['idFB'];
	$_SESSION['logged']['uname'] = $user['fb_username'];
	$_SESSION['logged']['name'] = $user['nama'];
	$_SESSION['logged']['kontak'] = $user['kontak'];
	$_SESSION['logged']['email'] = $user['email'];
	$_SESSION['logged']['token'] = $user['fb_access-token'];
	$facebook->setAccessToken($user['fb_access-token']);
	header('Location: '.PROFIL_URL."a/".$_POST['idfb']);	//redirect ke halaman sebelumnya
}else{	//login
	//header('Location: ' . $_GET['back']);	//redirect ke halaman sebelumnya
	// Get User ID from cookies
	$facebook->destroySession();	//mengamankan yang tertinggal
	$param['user'] = $facebook->getUser();	//cek kalau user sudah terdaftar
	//var_dump($param['user']);
	if ($param['user']) {	//kalau sudah terdaftar
	  try {
		// Proceed knowing you have a logged in user who's authenticated. ambil datanya
		$param['user_profile'] = $facebook->api('/me');		
		//cek udah ada di database gak ?
		$idfb = $param['user_profile']['id'];
		$check = checkUser($idfb);	//cek user terdaftar
		//$check = FALSE;	//belum terdaftar
		if ($check) {
			/*
			$_SESSION['logged']['token'] = $param['access_token'];
			$param['logoutUrl'] = $facebook->getLogoutUrl();	//ini logout FB taruh di template
			*/
			$user = getUser($idfb);
			$_SESSION['logged']['index'] = $user['idPengguna'];
			$_SESSION['logged']['id'] = $user['idFB'];
			$_SESSION['logged']['uname'] = $user['fb_username'];
			$_SESSION['logged']['name'] = $user['nama'];
			$_SESSION['logged']['kontak'] = $user['kontak'];
			$_SESSION['logged']['email'] = $user['email'];
			$_SESSION['logged']['token'] = $user['fb_access-token'];
			$facebook->setAccessToken($user['fb_access-token']);
			header('Location: ' . $_SERVER['HTTP_REFERER']);	//redirect ke halaman sebelumnya
		}else{
			$param['loginUrl'] = $facebook->getLoginUrl(array('scope' => 'email,user_about_me,user_location,user_activities,user_status,user_likes,publish_actions'));	//taruh di-template
			//redirect halaman login FB / permission app
			$param['access_token'] = $facebook->getAccessToken();
			view_register($param,$facebook);
			//die();
		}
	  } catch (FacebookApiException $e) {	//error
		error_log($e);
		$facebook->destroySession();
		$param['user'] = null;
	  }
		/*$user = getUser('1505350263');
		$_SESSION['logged']['index'] = $user['idPengguna'];
		$_SESSION['logged']['id'] = $user['idFB'];
		$_SESSION['logged']['uname'] = $user['fb_username'];
		$_SESSION['logged']['name'] = $user['nama'];
		$_SESSION['logged']['kontak'] = $user['kontak'];
		$_SESSION['logged']['email'] = $user['email'];
		$_SESSION['logged']['token'] = $user['fb_access-token'];*/
		//header('Location: ' . $_SERVER['HTTP_REFERER']);	//redirect ke halaman sebelumnya
	}else{
		echo "Belum terdaftar di aplikasi FB";
		$param['loginUrl'] = $facebook->getLoginUrl(array('scope' => 'email,user_about_me,user_location,user_activities,user_status,user_likes,publish_actions'));	//taruh di-template
		header('Location: '.$param['loginUrl']);	//redirect ke halaman login
	}
}
?>
